﻿using AutoMapper;
using MediatR;
using Traceability.App.Api.Application.Command.Get;
using Traceability.App.Api.DTO;
using Traceability.App.Domain;
using Traceability.App.ErrorCode;
using Traceability.App.Instructure;

namespace Traceability.App.Api.Application.Handler.Get
{
    public class GetBaoHandler : IRequestHandler<GetBaoCommand, ApiResult<List<BaoDTO>>>
    {
        private readonly IBaseRepository<HealthreportModel> healthreportRepo;
        private readonly IMapper mapper;

        public GetBaoHandler(IBaseRepository<HealthreportModel> healthreportRepo,IMapper mapper) 
        {
            this.healthreportRepo = healthreportRepo;
            this.mapper = mapper;
        }
        public Task<ApiResult<List<BaoDTO>>> Handle(GetBaoCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<BaoDTO>> res = new ApiResult<List<BaoDTO>>();
            var health = healthreportRepo.GetAll().Where(x => x.Batch == request.Batch && x.IsDelete==false).ToList();
            res.Code = ApiEnum.Success;
            res.Data = mapper.Map<List<BaoDTO>>(health);
            res.Msg = "查询保健信息成功";
            return Task.FromResult(res);
        }
    }
}
